# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


class FeedDeliveryUtil < ActiveRecord::Base

  $LOG_CONSOLE = "logfile"
  $LOG_TOKEN ="FeedDelivery:"
  $LOG_TOKEN_VER ="Ver1.1:"

  @@delivery_type_daily = 1
  @@delivery_type_weekly = 2
  @@delivery_type_monthly = 3
  
  def self.deliver_now(feed_id)
    postupdates = FeedPostUpdate.find(:all, :conditions => [ "feed_id = ?",feed_id])
    feedsubscribers = FeedSubscriber.find(:all, :conditions => [ "feed_id = ?",feed_id])
    list=""
    feedsubscribers.each {|feedsubscriber| 
      list = list << ",#{feedsubscriber.email}"
     }
     
     list = list[1,list.length]
     #AjubyLogger.logInfo "list: #{list}" 
     Feedeliverynotify.deliver_feedeliverynotifysimpletemplate(list,postupdates, Feed.find(feed_id))
  end 

  def self.deliver_daily
    AjubyLogger.logInfo "\nDelivering daily feeds to subscribers!\n"
    logData("Delivering daily feeds to subscribers!")
    deliver_base(1)
  end 
  
  def self.deliver_weekly
    AjubyLogger.logInfo "\nDelivering weekly feeds to subscribers!\n"
    logData("Delivering weekly feeds to subscribers!")
    deliver_base(2)
  end 
  
  def self.deliver_monthly
    AjubyLogger.logInfo "\nDelivering monthly feeds to subscribers!\n"
    logData("Delivering monthly feeds to subscribers!")
    deliver_base(3)
  end 
  
  def self.deliver_base(delivery_type)
    @feeddeliveries = FeedDelivery.find(:all, :conditions => [ "delivery_type = ?", delivery_type])
    #@todo everything after this todo, fix it, clean it
    @feeddeliveries.each {|feeddelivery| 
    #postupdates = FeedPostUpdate.find_by_sql("SELECT * FROM feed_post_updates WHERE (feed_id in (#{list}))")
    #postupdates = FeedPostUpdate.find(:all, :conditions => [ "feed_id = ?", feeddelivery.feed_id])
    postupdates = FeedPostUpdate.find(:all, :conditions => "feed_id = #{feeddelivery.feed_id} AND is_alive=1")
    feedsubscribers = FeedSubscriber.find(:all, :conditions => [ "feed_id = ?", feeddelivery.feed_id])
    #feedsubscribers = FeedSubscriber.find_by_sql("SELECT * FROM feed_subscribers WHERE (feed_id in (#{list}))")
     cnt = 1
     list=""
      feedsubscribers.each {|feedsubscriber| 
      if(cnt==1)
        list = "#{feedsubscriber.email}"
      else  
        list = "#{list},#{feedsubscriber.email}"
      end  
      cnt = cnt +1;
     }
     if(list==nil)
      logData("No subscribers for #{feeddelivery.feed_id} yet!.")
     end  
     
     logData("About to deliver the feed  #{feeddelivery.feed_id} to #{list}")
     Feedeliverynotify.deliver_feedeliverynotifysimpletemplate(list,postupdates, Feed.find(feeddelivery.feed_id))
     # once the email is sent, update the feed post is_alive to 0, so that next its disabled
     update_feed_post(postupdates)
     }  
  end
  
    def self.update_feed_post(postupdates)
        postupdates.each {|postupdate| 
          postupdate.is_alive=0
          postupdate.save
        }
    end
    
    def self.logData(debugString)
    if($LOG_CONSOLE=='logfile')
      Odotcron.logInfo("#{$LOG_TOKEN} #{$LOG_TOKEN_VER}:"<<debugString)
    end  
  end
  
end
